<?php

/**
 * This is the model class for table "{{ipbanned}}".
 *
 * The followings are the available columns in table '{{ipbanned}}':
 * @property integer $zip_id
 * @property string $zip_ip1
 * @property string $zip_ip2
 * @property string $zip_ip3
 * @property string $zip_ip4
 * @property integer $zip_expiration
 * @property integer $inputtime
 */
class Ipbanned extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Ipbanned the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{ipbanned}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zip_expiration, inputtime', 'numerical', 'integerOnly'=>true),
			array('zip_ip1, zip_ip2, zip_ip3, zip_ip4', 'length', 'max'=>15),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zip_id, zip_ip1, zip_ip2, zip_ip3, zip_ip4, zip_expiration, inputtime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zip_id' => 'Zip',
			'zip_ip1' => 'Zip Ip1',
			'zip_ip2' => 'Zip Ip2',
			'zip_ip3' => 'Zip Ip3',
			'zip_ip4' => 'Zip Ip4',
			'zip_expiration' => 'Zip Expiration',
			'inputtime' => 'Inputtime',
		);
	}

	/**
	 * ����û�IP�Ƿ������¼
	 * @return boolen 
	 */
	public function ipCheck()
	{
		$ips = explode('.', Yii::app()->request->userHostAddress);
		$ip = $this->findByAttributes(array('zip_ip1'=>$ips[0],'zip_ip2'=>$ips[1],'zip_ip3'=>$ips[2],'zip_ip4'=>$ips[3]),'zip_expiration>:time',array(':time'=>time()));
		if(!$ip)
			return true;
		return false;
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zip_id',$this->zip_id);
		$criteria->compare('zip_ip1',$this->zip_ip1,true);
		$criteria->compare('zip_ip2',$this->zip_ip2,true);
		$criteria->compare('zip_ip3',$this->zip_ip3,true);
		$criteria->compare('zip_ip4',$this->zip_ip4,true);
		$criteria->compare('zip_expiration',$this->zip_expiration);
		$criteria->compare('inputtime',$this->inputtime);

		$criteria->order = " inputtime DESC ";//默认为时间倒序
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}